package com.lzh.gulimall.order.controller;

import com.lzh.gulimall.order.entity.OrderEntity;
import com.lzh.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * @ClassName RabbitController
 * @Description TODO
 * @Author liuzhihui
 * @Date 2020/9/3 15:32
 * @Version 1.0
 **/
@Slf4j
@RestController
public class RabbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg")
    public String sendMsg(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            if (i%2==0){
                OrderReturnReasonEntity orderReturnReasonEntity=new OrderReturnReasonEntity();
                orderReturnReasonEntity.setId(1L);
                orderReturnReasonEntity.setCreateTime(new Date());
                orderReturnReasonEntity.setName("嘻嘻"+i);
                //最后一个参数表示消息的唯一id
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.world",
                        orderReturnReasonEntity,new CorrelationData(UUID.randomUUID().toString()));
            }else {
                OrderEntity orderEntity=new OrderEntity();
                orderEntity.setId(1L);
                orderEntity.setBillContent("哈哈"+i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.world22",
                        orderEntity,new CorrelationData(UUID.randomUUID().toString()));
            }
        }
        log.info("消息发送完成");

        return "ok";
    }

}
